# -- coding: utf-8 --

import time
# 所有的工具类都应该是类方法

# 测试动作类，每一个方法对应着每一个功能的执行动作及获取实际结果
from wnboss01.tools import UITools, FileTools, DBTools


class Action:

    def setup(self):
        UITools.get_driver('Firefox')
        UITools.get('http://192.168.44.128:8080/WoniuBoss4.0/')
        UITools.input({"by": "NAME", "what": "userName"}, "WNCD000")
        UITools.input({"by": "NAME", "what": "userPass"}, "woniu123")
        UITools.click({"by": "CLASS_NAME", "what": "btn.btn-primary.btn-block.btn-save"})

    def teardown(self):
        UITools.click({"by": "CSS_SELECTOR", "what": "ul.nav:nth-child(1) > li:nth-child(2) > a:nth-child(1)"})

    # 点击新增设备按钮
    def click_add_assets_button(self, locators):
        UITools.click(locators['add_assets_button'])

    # 选择要添加的设备名称
    def select_assets_name(self, locators):
        UITools.select_option(locators['assets_name'])

    # 选择任意的设备类型
    def select_assets_type(self, locators):
        UITools.select_option(locators['assets_type'])

    # 输入条码
    def input_barcode(self, locators, add_assets_data):
        UITools.input(locators['barcode'], add_assets_data['barcode'])

    # 输入采购价
    def input_price(self, locators, add_assets_data):
        UITools.input(locators['price'], add_assets_data['price'])

    # 输入归属人
    def input_employee(self, locators, add_assets_data):
        UITools.input(locators['employee'], add_assets_data['employee'])
        time.sleep(1)
        UITools.click(locators['employee_select'])

    # 选择采购时间
    def select_time(self, locators):
        UITools.click(locators['calendar'])
        UITools.click(locators['time'])

    # 添加采购备注
    def input_note(self, locators, add_assets_data):
        UITools.input(locators['note'], add_assets_data['note'])

    # 选择归属
    def select_owner(self, locators):
        UITools.select_option(locators['owner'])

    # 点击保存按钮
    def click_save_button(self, locators):
        UITools.click(locators['assets_save'])

    # 输入查询条码
    def input_query_barcode(self, locators, query_assets_data):
        UITools.input(locators['query_barcode'], query_assets_data['barcode'])

    # 点击查询按钮
    def click_query_button(self, locators):
        UITools.click(locators['query_barcode_button'])

    # 判断列表信息是否存在
    def exists_list_info(self, locators):
        return UITools.exists(locators['assets_list_info'])

    # 获取列表信息中的记录总数
    def get_list_count(self, locators):
        info = UITools.get_element(locators['assets_list_info']).text
        import re
        return re.findall('总共 (.*?) 条记录', info)


    # 执行测试动作
    def add_assets(self, add_assets_data):
        self.setup()
        locators = FileTools.read_json('locator.json')
        UITools.get('http://192.168.44.128:8080/WoniuBoss4.0/assets')

        sql = 'SELECT count(assets_id) from assets'
        old = DBTools.query(sql)  # 获得没有添加设置之前的记录数

        self.click_add_assets_button(locators)  # 点击新增
        self.select_assets_name(locators)
        self.select_assets_type(locators)
        self.input_barcode(locators, add_assets_data)
        self.input_price(locators, add_assets_data)
        self.input_employee(locators, add_assets_data)
        self.select_time(locators)
        self.input_note(locators, add_assets_data)
        self.select_owner(locators)
        self.click_save_button(locators)
        time.sleep(2)
        if UITools.exists(locators['alert_button']):  # 如果结果为真，表示有弹出窗口
            UITools.refresh()

        # 获取测试结果
        new = DBTools.query(sql)  # 获得添加设备之后的记录数
        if new[0][0] - old[0][0] == 1:
            actual = '添加成功'
        else:
            actual = '添加失败'

        self.teardown()

        return actual

    def query_assets(self, query_assets_data):
        self.setup()
        locators = FileTools.read_json('locator.json')
        UITools.get('http://192.168.44.128:8080/WoniuBoss4.0/assets')
        self.input_query_barcode(locators, query_assets_data)
        self.click_query_button(locators)
        time.sleep(3)

        sql = 'select count(assets_id) from assets'  # 从数据库assets表中获取总记录数
        db_count = DBTools.query(sql)[0][0]
        # 页面上的这个元素因为不同的条件会被隐藏，但可以查询到
        list_count = self.get_list_count(locators)
        if len(list_count) == 0:
            actual = '没有查询到设备'
        elif list_count[0] == '1':
            actual = '查询到一条'
        elif list_count[0] == str(db_count):
            actual = '查询到全部设备'
        else:
            actual = '查询结果不正确'
        self.teardown()
        return actual


    def modify_assets(self, data):
        pass
